package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.service.DetailStageService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.businesstage.pojo.dto.StageDetailDTO;
import com.xbongbong.pro.businesstage.pojo.dto.StageExecutionEventGetDTO;
import com.xbongbong.pro.businesstage.pojo.dto.StageExecutionEventSaveDTO;
import com.xbongbong.pro.businesstage.pojo.dto.StageFinishWorkDTO;
import com.xbongbong.pro.businesstage.pojo.dto.StageGetDTO;
import com.xbongbong.pro.businesstage.pojo.dto.StageJumpDTO;
import com.xbongbong.pro.businesstage.pojo.vo.StageDetailVO;
import com.xbongbong.pro.businesstage.pojo.vo.StageExecutionEventGetVO;
import com.xbongbong.pro.businesstage.pojo.vo.StageExecutionEventSaveVO;
import com.xbongbong.pro.businesstage.pojo.vo.StageFinishWorkVO;
import com.xbongbong.pro.businesstage.pojo.vo.StageGetVO;
import com.xbongbong.pro.businesstage.pojo.vo.StageJumpVO;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @Auther: yingjianfeng
 * @Date: 2021/11/24 10:43
 * @Description:
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/mobile/detail/stage")
public class MobileDetailStageController {
    @Resource
    private DetailStageService detailStageService;
    @Resource
    private PaasRedisHelper paasRedisHelper;

    /**
     * description:  详情页获取所有阶段
     * date: 2021/11/21 8:33 下午
     *
     * @Author yingjf
     */
    @RequestMapping(value = "/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String stageGet(@RequestBody @Valid StageGetDTO stageGetAllDTO, BindingResult br) {
        XbbResponse<StageGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                StageGetVO stageGetVO = detailStageService.stageGet(stageGetAllDTO);
                response = new XbbResponse<>(stageGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * description:  阶段详情获取指定阶段详情
     * date: 2021/11/21 9:06 下午
     *
     * @Author yingjf
     */
    @RequestMapping(value = "/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String stageDetail(@RequestBody @Valid StageDetailDTO stagedetailDTO, BindingResult br) {
        XbbResponse<StageDetailVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                StageDetailVO stageDetailVO = detailStageService.stageDetail(stagedetailDTO);
                response = new XbbResponse<>(stageDetailVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * description:  完成任务/取消完成任务
     * date: 2021/11/21 9:06 下午
     *
     * @Author yingjf
     */
    @RequestMapping(value = "/finishWork", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String stageFinishWork(@RequestBody @Valid StageFinishWorkDTO stageFinishWorkDTO, BindingResult br) {
        XbbResponse<StageFinishWorkVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(stageFinishWorkDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(stageFinishWorkDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                StageFinishWorkVO stageFinishWorkVO = detailStageService.stageFinishWork(stageFinishWorkDTO);
                response = new XbbResponse<>(stageFinishWorkVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * description:: 执行事件图片/附件 新增/更新接口
     * date: 2021/11/25 2:06 下午
     *
     * @Author yingjf
     */
    @RequestMapping(value = "/executionEvent/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String finishWork(@RequestBody @Valid StageExecutionEventSaveDTO stageExecutionEventSaveDTO, BindingResult br) {
        XbbResponse<StageExecutionEventSaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(stageExecutionEventSaveDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(stageExecutionEventSaveDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                StageExecutionEventSaveVO stageFinishWorkVO = detailStageService.stageExecutionEventSave(stageExecutionEventSaveDTO);
                response = new XbbResponse<>(stageFinishWorkVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * description:: 执行事件字段编辑/编辑关联数据编辑get接口
     * date: 2021/11/25 2:06 下午
     *
     * @Author yingjf
     */
    @RequestMapping(value = "/executionEvent/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String finishWork(@RequestBody @Valid StageExecutionEventGetDTO stageExecutionEventGetDTO, BindingResult br) {
        XbbResponse<StageExecutionEventGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                StageExecutionEventGetVO stageExecutionEventGetVO = detailStageService.stageExecutionEventGet(stageExecutionEventGetDTO);
                response = new XbbResponse<>(stageExecutionEventGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * description:  阶段跳转
     * date: 2021/12/3 1:55 下午
     *
     * @Author yingjf
     */
    @RequestMapping(value = "/jump", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String jump(@RequestBody @Valid StageJumpDTO stageJumpDTO, BindingResult br) {
        XbbResponse<StageJumpVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(stageJumpDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(stageJumpDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                StageJumpVO stageJumpVO = detailStageService.stageJump(stageJumpDTO);
                response = new XbbResponse<>(stageJumpVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
